<script setup>
import { ref, computed } from 'vue';
const items = ref([
  {
    id: 1,
    text: "Provide Omni Channel Warehousing For German Electronic Cigarettes",
    content: "Optimizing warehouse space. Scheduling work shifts. Managing inventory. These are challenges every warehouse manager deals with to meet the demands of high-volume fulfillment operations.",
    img: "https://www.oss-global.de/assets/case-one-GoG6rpuw.png"
  },
  {
    id: 2,
    text: "Provide Omni Channel Warehousing For German Electronic Cigarettes",
    content: "Optimizing warehouse space. Scheduling work shifts. Managing inventory. These are challenges every warehouse manager deals with to meet the demands of high-volume fulfillment operations.",
    img: "https://www.oss-global.de/assets/case-two-BaNS08Jb.png"
  },
  {
    id: 3,
    text: "Provide Omni Channel Warehousing For German Electronic Cigarettes",
    content: "Optimizing warehouse space. Scheduling work shifts. Managing inventory. These are challenges every warehouse manager deals with to meet the demands of high-volume fulfillment operations.",
    img: "https://www.oss-global.de/assets/case-three-Jb_uCRq9.png"
  },
  {
    id: 4,
    text: "Provide Omni Channel Warehousing For German Electronic Cigarettes",
    content: "Optimizing warehouse space. Scheduling work shifts. Managing inventory. These are challenges every warehouse manager deals with to meet the demands of high-volume fulfillment operations.",
    img: "https://www.oss-global.de/assets/case-four-D2Ej-Zmq.png"
  },
  {
    id: 5,
    text: "Provide Omni Channel Warehousing For German Electronic Cigarettes",
    content: "Optimizing warehouse space. Scheduling work shifts. Managing inventory. These are challenges every warehouse manager deals with to meet the demands of high-volume fulfillment operations.",
    img: "https://www.oss-global.de/assets/case-one-GoG6rpuw.png"
  },
  {
    id: 5,
    text: "Provide Omni Channel Warehousing For German Electronic Cigarettes",
    content: "Optimizing warehouse space. Scheduling work shifts. Managing inventory. These are challenges every warehouse manager deals with to meet the demands of high-volume fulfillment operations.",
    img: "https://www.oss-global.de/assets/case-one-GoG6rpuw.png"
  },
]);

const currentIndex = ref(0);
const hoveredIndex = ref(-1);
const getBackgroundStyle = (url, index) => {
  return {
    backgroundImage: `url(${url})`,
    backgroundSize: 'cover',
    backgroundPosition: 'center',
  }
}
const trackStyle = computed(() => ({
  transform: `translateX(-${currentIndex.value * normalWidth})`,
  transition: 'transform 0.5s ease'
}));
// 修改：只显示两屏（0-1-2-3 和 1-2-3-4）
const visibleItems = computed(() => {
  return items.value.slice(currentIndex.value, currentIndex.value + 4);
});

const normalWidth = computed(() => {
  return hoveredIndex.value === -1 ? '25%' : 'calc((100% - 40%) / 3)';
});

const hoveredWidth = computed(() => '40%');


// 修改：限制轮播范围
const prev = () => {
  const track = document.querySelector('.slider-track');
console.log("track",track)
  if (currentIndex.value > 0) {
    currentIndex.value--;
    track.style.transform = `translateX(-${currentIndex.value * 0}%)`;
  }
};

const next = () => {
  const track = document.querySelector('.slider-track');
console.log("track",track)
  if (currentIndex.value < items.value.length - 4) { // 最多滑动到倒数第4个元素
    currentIndex.value++;
   track.style.transform = `translateX(-${currentIndex.value * 0}%)`;
  }
};
</script>
<template>
  <div class="project-case">
    <div class="title">Project Case</div>
    <div class="desc">
      Doan equipment applies to road construction and road maintenance, and has provided solutions for
      2000 + customers
    </div>
    <div class="carousel slider-track" :style="trackStyle">
      <div v-for="(item, index) in visibleItems" :key="item.id" class="carousel-item"
        :style="{ width: hoveredIndex === index ? hoveredWidth : normalWidth, backgroundImage: `url('${(item.img)}')` }"
        @mouseenter="hoveredIndex = index" @mouseleave="hoveredIndex = -1">
        <div class="text">
          {{ item.text }}
        </div>
        <div class="mask">
          <div class="text-con">
            <div class="title">
              {{ item.text }}
            </div>
            <div class="sub-desc">
              {{ item.content }}
            </div>
            <div class="btn">Learn more</div>
          </div>
        </div>
      </div>
    </div>
    <div class="btns">
      <div class="prev btn-pub" @click="prev" v-show="currentIndex !== 0">
        < </div>
          <div class="next btn-pub" @click="next" v-show="currentIndex !== items.length - 4"> > </div>
      </div>
    </div>
</template>

<style lang="scss" scoped>
/* 保持原有样式不变 */
.project-case {
  /*display: flex;
  align-items: center;
  justify-content: center;*/
  height: 100%;
  margin: 0 auto;
  padding: 81px 0px 121px 0px;
  position: relative;

  .slider-track {
    display: flex;
    transition: transform 0.5s ease;
    /* 平滑过渡动画 */
  }

  .title {
    font-weight: bold;
    font-size: 32px;
    text-align: center;
    color: #333;
  }

  .desc {
    width: 622px;
    height: 39px;
    font-size: 16px;
    color: #666666;
    line-height: 22px;
    text-align: center;
    margin: 20px auto 40px;
  }

  .carousel {
    display: flex;
    width: 100%;
    overflow: hidden;

    .carousel-item {
      height: 422px;
      display: flex;
      transition: width 0.3s ease;
      flex-shrink: 0;
      overflow: hidden;
      position: relative;
      background-size: cover;
      background-repeat: no-repeat;

      .mask {
        opacity: 0;
        width: 100%;
        height: 100%;
        border-top: 6px solid #FF77AE;

        .text-con {
          width: 538px;
          padding: 90px 50px 0px 36px;

          .title {
            font-weight: bold;
            font-size: 22px;
            color: #fff;
            line-height: 36px;
            text-align: left;
          }

          .sub-desc {
            color: #fff;
            font-size: 14px;
            line-height: 20px;
            text-align: left;
            margin: 20px 0px 0px 0px;
          }

          .btn {
            color: #fff;
            width: 167px;
            height: 47px;
            background: #FF7700;
            border-radius: 2px;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            margin: 52px 0px 0px 0px;
          }
        }
      }

      .text {
        height: 145px;
        background: linear-gradient(180deg, rgba(0, 0, 0, 0) 0%, rgba(0, 0, 0, 0.73) 100%);
        display: flex;
        align-items: center;
        text-align: left;
        font-weight: bold;
        font-size: 16px;
        color: #fff;
        padding: 0px 10px;
        position: absolute;
        bottom: 0;
      }

      &:hover {

        .text {
          display: none;
        }

        .mask {
          position: relative;
          opacity: 1;
          z-index: 1;

          &::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: linear-gradient(267deg, #8252A2 0%, #5B108E 100%);
            opacity: 0.8;
            z-index: -1;
          }
        }
      }
    }
  }
}



.btns {
  width: 100%;
  text-align: center;
  position: absolute;
  bottom: 30px;
  display: flex;
  justify-content: center;
  column-gap: 21px;

  .btn-pub {
    width: 50px;
    height: 50px;
    text-align: center;
    line-height: 50px;
    border-radius: 6px;
    border: 1px solid #5B108E;
    color: #5B108E;
    cursor: pointer;
    font-size: 22px;
  }
}

.btn-pub:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}
</style>
